<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="月份" prop="configName">
        <el-date-picker
            v-model="queryParams.yearMonth"
            type="month"
            value-format="YYYY-MM"
            placeholder="选择月份"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configList">
      <el-table-column align="center" type="index" label="序号" width="55"/>
      <el-table-column align="center" label="姓名" prop="userName"/>
      <el-table-column align="center" label="类型" prop="ftype">
        <template #default="scope">
          <span v-if="scope.row.ftype === 1">事假</span>
          <span v-else-if="scope.row.ftype === 2">年假</span>
          <span v-else-if="scope.row.ftype === 3">病假</span>
          <span v-else-if="scope.row.ftype === 4">调休</span>
          <span v-else-if="scope.row.ftype === 5">产假</span>
          <span v-else-if="scope.row.ftype === 6">陪产假</span>
          <span v-else-if="scope.row.ftype === 7">婚假</span>
          <span v-else-if="scope.row.ftype === 8">丧假</span>
          <span v-else-if="scope.row.ftype === 9">探亲假</span>
          <span v-else-if="scope.row.ftype === 101">出差</span>
          <span v-else-if="scope.row.ftype === 102">外出</span>
          <span v-else-if="scope.row.ftype === 103">其他</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="天数" prop="daynum"/>
      <el-table-column align="center" label="小时数" prop="hournum"/>
      <el-table-column align="center" label="开始时间" prop="sd"/>
      <el-table-column align="center" label="结束时间" prop="ed"/>
    </el-table>

    <pagination
        v-show="total > 0"
        v-model:limit="queryParams.ps"
        v-model:page="queryParams.pn"
        :total="total"
        @pagination="getList"
    />
  </div>
</template>

<script name="Holiday" setup>
import {
  queryHolidayList2,
} from "@/api/system/config";
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
const {proxy} = getCurrentInstance();
const {sys_yes_no} = proxy.useDict("sys_yes_no");

const configList = ref([]);
const open = ref(false);
const openDetail = ref(false);
const holidayData = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pn: 1,
    ps: 10,
    yearMonth: undefined,
    userName: undefined
  },
});

const {queryParams, form, rules} = toRefs(data);

/** 查询参数列表 */
function getList() {
  loading.value = true;
  queryHolidayList2({
    ...queryParams.value,
    fyear: queryParams.value.yearMonth?.split('-')[0],
    fmonth: queryParams.value.yearMonth?.split('-')[1]
  }).then(response => {
    configList.value = response.data.list
    total.value = response.data.total
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pn = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 导出按钮操作 */
function handleExport() {
  const tHeader = [
    '姓名', '类型', '天数', '小时数', '开始时间', '结束时间'
  ]
  const filterVal = [
    'userName', 'ftype', 'daynum', 'hournum', 'sd', 'ed',
  ]
  const DATA = {
    pn: 1,
    ps: 100000,
    fyear: queryParams.value.yearMonth?.split('-')[0],
    fmonth: queryParams.value.yearMonth?.split('-')[1]
  }
  queryHolidayList2(DATA).then(response => {
    let list = response.data.list
    list.map(res => {
      if(res.ftype === 1) {
        res.ftype = '事假'
      } else if(res.ftype === 2) {
        res.ftype = '年假'
      } else if(res.ftype === 3) {
        res.ftype = '病假'
      } else if(res.ftype === 4) {
        res.ftype = '调休'
      } else if(res.ftype === 5) {
        res.ftype = '产假'
      } else if(res.ftype === 6) {
        res.ftype = '陪产假'
      } else if(res.ftype === 7) {
        res.ftype = '婚假'
      } else if(res.ftype === 8) {
        res.ftype = '丧假'
      } else if(res.ftype === 9) {
        res.ftype = '探亲假'
      } else if(res.ftype === 101) {
        res.ftype = '出差'
      } else if(res.ftype === 102) {
        res.ftype = '外出'
      } else if(res.ftype === 103) {
        res.ftype = '其他'
      }
    })
    const data = formatJson(filterVal, list)
    export_json_to_excel(tHeader, data, '请假记录表')
  });
}

getList();
</script>
